package com.hertz.iris.core;

import java.util.List;

import com.hertz.iris.IrisApplication;
import com.hertz.iris.dao.DaoSession;
import com.hertz.iris.dao.MatchAlarmDao;
import com.hertz.iris.dao.NormalAlarmDao;
import com.hertz.iris.pojo.MatchAlarm;
import com.hertz.iris.pojo.NormalAlarm;

public class DBService {
	
	private static DBService instance;
	private DaoSession mDaoSession;
	private NormalAlarmDao normalAlarmDao;
	private MatchAlarmDao matchAlarmDao;
	
	private DBService() {
	}

	public static DBService getInstance() {
		if (instance == null) {
			instance = new DBService();
			instance.mDaoSession = IrisApplication.getInstance().getDaoSession();
			instance.normalAlarmDao = instance.mDaoSession.getNormalAlarmDao();
			instance.matchAlarmDao = instance.mDaoSession.getMatchAlarmDao();
		}
		return instance;
	}
	
	
	public NormalAlarm loadNormalAlarm(long id) {
		return normalAlarmDao.load(id);
	}
	
	public MatchAlarm loadMatchAlarm(long id){
		return matchAlarmDao.load(id);
	}
	
	public MatchAlarm loadMatchAlarmByMid(long mid){
		List<MatchAlarm> result = matchAlarmDao.queryRaw("where " + MatchAlarmDao.Properties.Mid.columnName + "=?",String.valueOf(mid));
		if(result != null && result.size() > 0)
			return result.get(0);
		else
			return null;
	}
	
	public List<MatchAlarm> loadMatchAlarmByStatus(byte status){
		List<MatchAlarm> result = matchAlarmDao.queryRaw("where " + MatchAlarmDao.Properties.Status.columnName + "=?",String.valueOf(status));
		if(result != null && result.size() > 0)
			return result;
		else
			return null;
	}
	
	public List<NormalAlarm> loadAllNormalAlarm(){
		return normalAlarmDao.loadAll();
	}
	
	/**
	 * query list with where clause
	 * ex: begin_date_time >= ? AND end_date_time <= ?
	 * @param where where clause, include 'where' word
	 * @param params query parameters
	 * @return
	 */
	
	public List<NormalAlarm> queryNormalAlarm(String where, String... params){
		return normalAlarmDao.queryRaw(where, params);
	}
	
	
	/**
	 * insert or update alarm
	 * @param note
	 * @return insert or update note id
	 */
	public long saveNormalAlarm(NormalAlarm normalAlarm){
		return normalAlarmDao.insertOrReplace(normalAlarm);
	}
	
	public long saveMatchAlarm(MatchAlarm matchAlarm){
		return matchAlarmDao.insertOrReplace(matchAlarm);
	}
	
	public void saveMatchAlarmLists(final List<MatchAlarm> list){
		if(list == null || list.isEmpty()){
		     return;
	    }
		matchAlarmDao.getSession().runInTx(new Runnable() {
			@Override
			public void run() {
				for(int i=0; i<list.size(); i++){
					MatchAlarm matchAlarm = list.get(i);
					matchAlarmDao.insertOrReplace(matchAlarm);
				}
			}
		});
	}
	
	/**
	 * insert or update NormalAlarmList use transaction
	 * @param list
	 */
	public void saveNormalAlarmLists(final List<NormalAlarm> list){
	    	if(list == null || list.isEmpty()){
			     return;
		    }
	    	normalAlarmDao.getSession().runInTx(new Runnable() {
			@Override
			public void run() {
				for(int i=0; i<list.size(); i++){
					NormalAlarm normalAlarm = list.get(i);
					normalAlarmDao.insertOrReplace(normalAlarm);
				}
			}
		});
		
	}
	
	/**
	 * delete all NormalAlarm
	 */
	public void deleteAllNormalAlarm(){
		normalAlarmDao.deleteAll();
	}
	
	/**
	 * delete all MatchAlarm
	 */
	public void deleteAllMatchAlarm(){
		matchAlarmDao.deleteAll();
	}
	
	/**
	 * delete NormalAlarm by id
	 * @param id
	 */
	public void deleteNormalAlarm(long id){
		normalAlarmDao.deleteByKey(id);
	}
	
	public void deleteNormalAlarm(NormalAlarm normalAlarm){
		normalAlarmDao.delete(normalAlarm);
	}
	public void deleteMatchAlarm(MatchAlarm matchAlarm){
		matchAlarmDao.delete(matchAlarm);
	}
}
